﻿// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="WebDrivers.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.ShareTest.Core
{
    #region

    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Remote;

    #endregion

    /// <summary>
    /// 	TODO: Update summary.
    /// </summary>
    public class WebDrivers
    {
        #region Constants and Fields

        private static RemoteWebDriver centralAdminBrowser;

        private static RemoteWebDriver mainSiteBrowser;

        #endregion

        #region Public Properties

        public static RemoteWebDriver CentralAdminBrowser
        {
            get
            {
                if (centralAdminBrowser == null)
                {
                    centralAdminBrowser = StartAndConfigBrowser();
                }
                return centralAdminBrowser;
            }
            set
            {
                centralAdminBrowser = value;
            }
        }

        public static RemoteWebDriver MainSiteBrowser
        {
            get
            {
                if (mainSiteBrowser == null)
                {
                    mainSiteBrowser = StartAndConfigBrowser();
                }
                return mainSiteBrowser;
            }
            set
            {
                mainSiteBrowser = value;
            }
        }

        #endregion

        #region Public Methods

        public static RemoteWebDriver GetNewBrowser()
        {
            return StartAndConfigBrowser();
        }

        public static void ResetBrowsers()
        {
            CentralAdminBrowser = null;
            MainSiteBrowser = null;
        }

        /// <summary>
        /// 	Starts the relevant browset
        /// </summary>
        /// <returns>
        /// 	An instance of a WebDriver (or browser effectively).
        /// </returns>
        public static RemoteWebDriver StartAndConfigBrowser()
        {
            RemoteWebDriver driver = null;
            switch (RunSettings.WebBrowser)
            {
                case "firefox":
                    FirefoxProfile firecFirefoxProfile = new FirefoxProfile { AcceptUntrustedCertificates = true };
                    driver = new FirefoxDriver(firecFirefoxProfile);
                    break;
                case "iexplore":

                    InternetExplorerOptions options = new InternetExplorerOptions
                        { IntroduceInstabilityByIgnoringProtectedModeSettings = true };
                    driver = new InternetExplorerDriver(options);

                    break;
                case "chrome":
                    driver = new ChromeDriver();
                    break;
            }

            if (RunSettings.DriverTimeout.HasValue)
            {
                if (driver != null)
                {
                    driver.Manage().Timeouts().ImplicitlyWait(RunSettings.DriverTimeout.Value);
                }
            }
            return driver;
        }

        #endregion
    }
}